#include "yumi.h"

extern OgreCanvas * gc;
void Yumi::spriteMove(int _xx, int _yy)
{

}
void Yumi::update(Ogre::Real timeSinceLast)
{
	//if no life, set vanish
	if(mLifeLeft > 0)
	{
		mLifeLeft -= timeSinceLast;
		setVisable(true);
	}
	else
	{
		setVisable(false);
		mLifeLeft = 0;
		setNeedSynchronize(false);
		setFXDisabled();
		setVanishDisabled();
		mLinearVelocityFX = btVector3(0,0,0);
	}

	//vanish state checking
	if(mVanishStateEnable&&mVanishTimeLeft>0)
		_vanishProcess(timeSinceLast);



	/*m_pDynamicsWorld->synchronizeSingleMotionState(m_pRigidBody);
	m_pRigidBody->getMotionState()->getWorldTransform(m_worldTrans);
	btQuaternion rot = m_worldTrans.getRotation();
	btVector3 pos = m_worldTrans.getOrigin();
	
	char posY[100];
	sprintf(posY, "visable:%d, mVanishStateEnable:%d, vanishTimeLeft:%f", 
		mVisable,mVanishStateEnable,mVanishTimeLeft);
	gc->drawString(400,125, posY);
	sprintf(posY, "position:%f,%f,%f",pos.x(),
		pos.y(),
		pos.z());
	gc->drawString(400,100, posY);

	sprintf(posY, "dynamicWorld:%d", m_pDynamicsWorld);
	gc->drawString(400,150, posY);*/
}

void Yumi::reborn()
{
	mLifeLeft = mLife;
}

void Yumi::kill()
{
	mLifeLeft = 0;
}

bool Yumi::isAlive()
{
	return mLifeLeft>0 ? true:false;
}

void Yumi::eventClick(int _xx, int _yy)
{
	Ogre::Vector3 _p3D = Sprite::_getInteractPosition3D(m_pCamera,Vector2(_xx,_yy), height);
	setPosition(_p3D);
	reborn();
	setVisable(true);
	setNeedSynchronize(false);
	setVanishDisabled();
	setFXDisabled();
	mLinearVelocityFX = btVector3(0,0,0);

	
	
}

void Yumi::eventMouseRelease(int _xx, int _yy)
{
	Ogre::Vector3 _p3D = Sprite::_getInteractPosition3D(m_pCamera,Vector2(_xx,_yy), height);
	setPosition(_p3D);
	/*m_pRigidBody->setLinearVelocity(btVector3(50.0f * (Ogre::Math::UnitRandom() * 2.0f -1),
		50.0f * (Ogre::Math::UnitRandom() * 2.0f - 1),
		0));*/

	m_pRigidBody->setLinearVelocity(mLinearVelocityFX);
	setFXEnabled();
	setNeedSynchronize(true);
	reborn();
	setVanish(3.0f);

}

void Yumi::eventMouseMove(int _x, int _y, int _xRel, int _yRel, bool isButtonDown)
{
	if(isButtonDown)
	{
		Ogre::Vector3 _p3D = Sprite::_getInteractPosition3D(m_pCamera,Vector2(_x,_y), height);
		setPosition(_p3D);
		mLinearVelocityFX =mLinearVelocityFX + btVector3((float)_xRel/10.0f,0,(float)_yRel/10.0f);
		setVisable(true);
		setNoTransparent();
		setVanishDisabled();
		setNeedSynchronize(false);
		reborn();
		setFXDisabled();
	}

}

void Yumi::stateMouseDown(int _x, int _y, int _xRel, int _yRel, bool isButtonDown)
{

}